package com.kx.ssm.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器，判断用户有没有登录，如果登录直接放行
 *
 * @ClassName LoginInterceptor
 * @Author Sun
 * @Date 2021/5/23 15:10
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断用户有没有登陆,如果登陆了则放行
        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        //请求方式是 post 地址是注册地址 如何和我们地址是一样的则就是注册操作
        if ("post/users/user".equalsIgnoreCase(method + requestURI)) {
            //放行
            return true;
        }
        //已经登录直接放行
        return request.getSession().getAttribute("user") != null;
    }
}
